<template>
  <div class="store-card" :class="{ 'is-error': validateErrorTip && editMode }">
    <div class="header">
      <div class="title">{{ storeInfo.siteName }}</div>
      <div v-if="validateErrorTip && editMode" class="error_text">{{ validateErrorTip }}</div>
    </div>
    <div class="content">
      <div v-if="isNoCarSteward || editMode">
        <div class="edit-mode" v-if="editMode">
          <a-scrollbar
            ref="scrollContainer"
            :outer-style="{ 'overflow-y': 'auto', 'overflow-x': 'hidden' }"
            style="max-height: 143px; overflow-y: auto; overflow-x: hidden"
            @scroll="scrollFn"
          >
            <div class="store-card-item">
              <template v-for="(_, index) in storeInfo.carHousekeepList">
                <StaffSelect
                  popupContainer="body"
                  :isOpenCarHousekeep="carStewardStatus.isOpenCarHousekeep"
                  ref="staffSelectRef"
                  :carHousekeepList="storeInfo.carHousekeepList"
                  :index="index"
                  :storeInfo="storeInfo"
                  @change="handleChange"
                />
              </template>
            </div>
          </a-scrollbar>
          <a-link class="select-none" v-if="storeInfo.carHousekeepList.length < 20" @click="addStoreFn">添加车管家</a-link>
        </div>
        <a-scrollbar
          :outer-style="{ 'overflow-y': 'auto', 'overflow-x': 'hidden' }"
          style="max-height: 143px; overflow-y: auto; overflow-x: hidden"
          @scroll="scrollFn"
          v-else
        >
          <div class="view-mode">
            <template v-for="(staff, sIndex) in storeInfo.carHousekeepList" :key="sIndex">
              <div v-if="staff.staffId" class="staff-item" :class="{ b_border: sIndex < storeInfo.carHousekeepList.length - 1 }">
                <span class="text">{{ staff.staffName }} | {{ staff.staffPhone }}</span>
                <span class="error_text" v-if="staff.staffStatusName">（{{ staff.staffStatusName }}）</span>
              </div>
            </template>
          </div>
        </a-scrollbar>
      </div>
      <!-- 没有数据 -->
      <div class="no-data" v-else>
        <div class="title">未配置</div>
        <div>未配置车管家，将无法正常派单，</div>
        <div>为避免影响正常操作，请尽快完成配置</div>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { inject, computed, ref, nextTick } from 'vue'
  import StaffSelect from './staff-select.vue'

  const carStewardStatus = inject('carStewardStatus')
  const props = defineProps({
    // 店铺索引
    index: Number,
  })
  const emits = defineEmits(['validateFn', 'addStoreListFn'])

  const scrollContainer = ref()
  // 触发父组件新增车管家函数
  const addStoreFn = () => {
    emits('addStoreListFn', props.index)
    // 添加车管家后滚动到最底部
    nextTick(() => {
      const container = scrollContainer.value.$el.querySelector('.store-card-item')
      container && scrollContainer.value.scrollTop(container.scrollHeight)
    })
  }

  // 门店信息
  const storeInfo = computed(() => {
    return carStewardStatus.storeList[props.index]
  })
  const validateErrorTip = computed(() => {
    const is_err = carStewardStatus.errSiteIdList.includes(storeInfo.value.siteId)
    if (is_err == false) return
    const array = storeInfo.value.carHousekeepList
    const index = array.findIndex((item) => item.staffStatusName == '选择重复，请重新选择')
    if (index !== -1) {
      return '选择重复，请重新选择'
    }
    return '当前门店无生效的车管家，请重新分配'
  })

  // 编辑模式
  const editMode = computed(() => {
    return carStewardStatus.editMode
  })

  // 是否有车管家
  const isNoCarSteward = computed(() => {
    const array = storeInfo.value.carHousekeepList
    for (let index = 0; index < array.length; index++) {
      const element = array[index]
      if (element.staffId) {
        return true
      }
    }
    return false
  })
  // 滑动滚动条 控制下拉框的隐藏
  const staffSelectRef = ref(null)
  const scrollFn = () => {
    staffSelectRef.value.length && staffSelectRef.value.map((it) => it.popupVisibleFn())
  }
  const handleChange = () => {
    emits('validateFn')
  }
</script>

<style lang="less" scoped>
  .store-card {
    min-width: 380px;
    background: #fff;
    border-radius: 2px;
    border: 1px solid #e5e8ef;
    margin-bottom: 20px;
    margin-right: 10px;

    &.is-error {
      .header {
        background: #fde1e0;
      }
    }

    .error_text {
      font-size: 12px;
      color: #f76964;
    }

    .header {
      height: 46px;
      width: 100%;
      background: #f2f3f5;
      padding: 0 16px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      border-radius: 2px 2px 0 0;

      .title {
        font-weight: 500;
        font-size: 14px;
        color: #1d2129;
        line-height: 22px;
        width: 100%;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      .btn-wrap {
        flex-shrink: 0;
        display: flex;
        .btn-item {
          margin-left: 12px;
          font-size: 14px;
          color: #1d2129;
          line-height: 22px;
          cursor: pointer;
          &:hover {
            font-weight: 500;
          }

          .icon {
            font-size: 16px;
            margin-top: -2px;
          }
        }
      }
    }
    .content {
      min-height: 160px;
      width: 100%;

      .content-item {
        max-height: 143px;
        overflow-y: auto;
      }

      .view-mode {
        padding: 0 16px;
        .staff-item {
          height: 50px;
          padding: 16px 0;
          font-weight: 400;
          font-size: 14px;
          color: #1d2129;
          line-height: 16px;
          .icon {
            margin-left: 12px;
            display: inline-block;
            width: 36px;
            height: 18px;
            background: #ffdede;
            border-radius: 2px 2px 2px 2px;
            border: 1px solid #f76964;
            text-align: center;
            font-size: 12px;
            color: #f76964;
            line-height: 18px;
          }

          &.b_border {
            border-bottom: 1px solid #f2f3f5;
          }
        }
      }
      .edit-mode {
        padding: 16px;
        .select-item {
          position: relative;
          margin-bottom: 16px;
          .error_wrap {
            position: absolute;
            left: 0;
            bottom: -16px;
          }
        }
      }

      .no-data {
        text-align: center;
        padding-top: 43px;
        .title {
          font-weight: 400;
          font-size: 14px;
          color: #f76964;
          line-height: 20px;
          margin-bottom: 3px;
        }
        font-weight: 400;
        font-size: 12px;
        color: #86909c;
        line-height: 17px;
      }
    }
  }
</style>
